package de.pharmacontrol.psa.beans.managed;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.model.SelectItem;

import de.pharmacontrol.pilot.entities.LogEntry;
import de.pharmacontrol.pilot.entities.LogEntry.Type;
import de.pharmacontrol.psa.beans.interfaces.LogEntryService;

@ManagedBean
@ApplicationScoped
public class LogManagedBean {

	@EJB
	LogEntryService logBean;

	private String searchPattern;

	private final static SelectItem[] typeOptions;
	
	static {
		typeOptions = new SelectItem[LogEntry.Type.values().length+1];
		typeOptions[0] = new SelectItem("", "Select"); 
		int i = 1;
		for (Type type : LogEntry.Type.values()){
			typeOptions[i++]= new SelectItem(type);
		}
	}


	public LogManagedBean() {
	}

	public String getSearchPattern() {
		return searchPattern;
	}

	public void setSearchPattern(String searchPattern) {
		this.searchPattern = searchPattern;
	}

	private List<LogEntry> searchLogResults;
	private LogEntry selectedLogEntry;

	public String searchPattern() {
		String pattern = (this.searchPattern == null) ? "" : this.searchPattern.trim();
		this.searchLogResults = logBean.findAllLogEntries(pattern, null, null);
		return pattern;
	}

	public List<LogEntry> getSearchLogResults() {
		return searchLogResults;
	}

	public void setSearchLogResults(List<LogEntry> searchLogResults) {
		this.searchLogResults = searchLogResults;
	}

	public LogEntry getSelectedLogEntry() {
		return selectedLogEntry;
	}

	public void setSelectedLogEntry(LogEntry selectedLogEntry) {
		this.selectedLogEntry = selectedLogEntry;
	}


	public SelectItem[] getTypeOptions() {
		return typeOptions;
	}

}
